﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.User
{
    public partial class BookDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel1.Visible = false;
                GetInfo();
            }
        }

        private void GetInfo()
        {
            try
            {
                string productID = Request.QueryString["productid"];

                ProductsBusiness pb = new ProductsBusiness();
                DataTable dt = pb.GetDataDetails(productID);

                LblProductName.Text = dt.Rows[0]["productname"].ToString();
                LblDescription.Text = dt.Rows[0]["description"].ToString();
                LblPrice.Text = dt.Rows[0]["price"].ToString();
                LblStockQuantity.Text = dt.Rows[0]["stockquantity"].ToString();
                LblSalesQuantity.Text = dt.Rows[0]["salesquantity"].ToString();
                Image1.ImageUrl = dt.Rows[0]["image"].ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void BtnCart_Click(object sender, EventArgs e)
        {
            try
            {
                CartsEntity ce = new CartsEntity();
                ce.Username = Session["UserName"].ToString();
                ce.Productname = LblProductName.Text;
                ce.Quantity = Convert.ToInt32(TxtBuyNum.Text);
                ce.Price = Convert.ToDecimal(LblPrice.Text);

                CartsBusiness cb = new CartsBusiness();
                int res = cb.AddCart(ce);
                if (res > 0)
                {
                    Response.Write("<script>alert('添加成功！'),location.href='UserMain.aspx'</script>");
                }
                else
                {
                    Response.Write("<script>alert('添加失败！')</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnBuy_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            BindUserInfo();
        }

        private void BindUserInfo()
        {
            try
            {
                UsersEntity ue = new UsersEntity();
                ue.Username = Session["UserName"].ToString();

                UsersBusiness ub = new UsersBusiness();
                DataTable dt = ub.GetUserInfo(ue);

                TxtUserName.Text = dt.Rows[0]["username"].ToString();
                TxtAddress.Text = dt.Rows[0]["address"].ToString();
                TxtTelephone.Text = dt.Rows[0]["telephone"].ToString();
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }

        protected void BtnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                OrdersEntity oe = new OrdersEntity();
                oe.Username = Session["UserName"].ToString();
                oe.Amount = Convert.ToDecimal(LblPrice.Text) * Convert.ToInt32(TxtBuyNum.Text);
                oe.Orderdate = DateTime.Now;

                OrdersBusiness ob = new OrdersBusiness();
                int res1 = ob.AddOrder(oe);

                if (res1 > 0)
                {
                    object orderID = ob.GetMaxOrderID();

                    OrderdetailsEntity ode = new OrderdetailsEntity();
                    ode.Orderid = Convert.ToInt32(orderID);
                    ode.Username = Session["UserName"].ToString();
                    ode.Productname = LblProductName.Text;
                    ode.Quantity = Convert.ToInt32(TxtBuyNum.Text);
                    ode.Amount = Convert.ToInt32(TxtBuyNum.Text) * Convert.ToDecimal(LblPrice.Text);

                    OrderdetailsBusiness odb = new OrderdetailsBusiness();
                    int res2 = odb.AddOrderdetail(ode);
                    if (res2 > 0)
                    {
                        ProductsEntity pe = new ProductsEntity();
                        pe.Productid = Convert.ToInt32(Request.QueryString["productid"]);
                        pe.Salesquantity = Convert.ToInt32(TxtBuyNum.Text);

                        ProductsBusiness pb = new ProductsBusiness();
                        int res3 = pb.ResetProduct(pe);

                        if (res3 > 0)
                        {
                            Response.Write("<script>alert('购买成功！'),location.href='UserMain.aspx'</script>");
                        }
                    }

                }
                else
                {
                    Response.Write("<script>alert('购买失败！')</script>");
                }
            }
            catch
            {
                Response.Write("<script>alert('请先登录！'),location.href='..//UserLogin.aspx'</script>");
            }
        }
    }
}